//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    listData: [],
    maskFlag: true,
    // 操作id
    actionId: "",
    // 
    isHalfPower: "",
    payFlag: true,
    payMoney: "",
    orderNumber: "",
    payType: "",
    // 
    uid: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.GetUserInfoByID();
    this.GetGoodsList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.GetUserInfoByID();
    this.GetGoodsList();
  },

  
  //显示支付密码输入层
  showInputLayer: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function () {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function (e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function () {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=BusinessHalfPowerPay&OrderNumber=" + orderNumber + "&PayType=0"),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                that.GetUserInfoByID();
                that.GetGoodsList();
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // upload
  GetGoodsList: function () {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetGoodsList&UID=" + uid),
      success: function (res) {
        listData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < listData.length; i++) {
          listData[i].Images = listData[i].Images.split(';')[0];
          listData[i].actionFlag = true;
          // listData[0].actionFlag = false;
        }
        that.setData({
          listData: listData
        })
        // console.log(listData);
      }
    })
  },
  // GetUserInfoByID
  GetUserInfoByID: function () {
    var that = this;
    var uid = this.data.uid;
    var isHalfPower = this.data.isHalfPower;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByID&UID=" + uid),
      success: function (res) {
        isHalfPower = JSON.parse(baseJs.decryUrl(res.data)).ResultData.IsHalfPower;
        that.setData({
          isHalfPower: isHalfPower
        })
        // console.log(isHalfPower);
      }
    })
  },
  // actionTap
  actionTap: function (e) {
    var that = this;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    for (var i = 0; i < listData.length; i++) {
      listData[e.currentTarget.dataset.index].actionFlag = false;
    }
    this.setData({
      maskFlag: false,
      listData: listData,
      actionId: e.currentTarget.dataset.index,
    })
  },
  // maskTap
  maskTap: function () {
    var that = this;
    var listData = this.data.listData;
    for (var i = 0; i < listData.length; i++) {
      listData[i].actionFlag = true;
    }
    this.setData({
      maskFlag: true,
      listData: listData
    })
  },
  // upGoods产品上架
  upGoods: function (e) {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=OnlineGoods&UID=" + uid + "&GoodsId=" + e.currentTarget.dataset.id),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        // console.log(json);
        if (json.ResultFlag == 1) {
          that.setData({
            maskFlag: true,
            listData: listData,
          })
          that.GetGoodsList();
        }
      }
    })
  },
  // downGoods产品下架
  downGoods: function (e) {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=OfflineGoods&UID=" + uid + "&GoodsId=" + e.currentTarget.dataset.id),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        // console.log(json);
        if (json.ResultFlag == 1) {
          that.setData({
            maskFlag: true,
            listData: listData,
          })
          that.GetGoodsList();
        }
      }
    })
  },
  // delGoods产品删除
  delGoods: function (e) {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    wx.showModal({
      title: '提示',
      content: '确认删除该商品？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=DeleteGoods&GoodsId=" + e.currentTarget.dataset.id),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              // console.log(json);
              if (json.ResultFlag == 1) {
                listData.splice(e.currentTarget.dataset.index, 1);
                wx.showToast({
                  title: "删除成功",
                  icon: 'none',
                  duration: 2000
                });
                that.setData({
                  maskFlag: true,
                  listData: listData,
                })
                // that.GetGoodsList();
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消');
        }
      }
    })
  },
  // updateGoods修改
  updateGoods: function (e) {
    this.setData({
      maskFlag: true,
    })
    wx.navigateTo({
      url: '/packageA/uploadGoods/uploadGoods?addOrUpdate=update&goodsId=' + e.currentTarget.dataset.id,
    })

  },
  // applyDiscount申请折扣
  applyDiscount: function (e) {
    var that = this;
    var uid = this.data.uid;
    var isHalfPower = this.data.isHalfPower;
    var payMoney = this.data.payMoney;
    var orderNumber = this.data.orderNumber;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    if (isHalfPower == 0) {
      wx.showModal({
        title: '提示',
        content: '暂时没有申请成为折扣产品权限，请购买权限',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=CreateOrderToBusinessHalfPower&UID=" + uid),
              success: function (res) {
                var json = JSON.parse(baseJs.decryUrl(res.data));
                // console.log(json);
                // 弹出支付
                orderNumber = json.ResultData.OrderNumber;
                payMoney = parseFloat(json.ResultData.PayRmb).toFixed(2);
                that.setData({
                  payFlag: false,
                  orderNumber: orderNumber,
                  payMoney: payMoney,
                  listData: listData,
                })
              }
            })
          } else if (res.cancel) {
            // console.log('用户点击取消');
          }
        }
      })
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=BusinessApplyHalfGoods&UID=" + uid + "&GoodsId=" + e.currentTarget.dataset.id),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          // console.log(json);
          if (json.ResultFlag == 1) {
            wx.showToast({
              title: json.ResultMsg + "请等待审核",
              icon: 'none',
              duration: 2000
            });
            that.setData({
              maskFlag: true,
              listData: listData,
            })
            that.GetGoodsList();
          }
        }
      })
    }

  },
  // cancelDiscount取消折扣
  cancelDiscount: function (e) {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BusinessCancleHalfGoods&UID=" + uid + "&GoodsId=" + e.currentTarget.dataset.id),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        // console.log(json);
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.setData({
            maskFlag: true,
            listData: listData,
          })
          that.GetGoodsList();
        }
      }
    })
  },
  // exchange同步至兑换专区
  exchange: function (e) {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    wx.showModal({
      title: '提示',
      content: '该商品同步至兑换专区？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=GoodsSetArea&GoodsId=" + id + "&Type=1"),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              // console.log(json);
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                that.setData({
                  maskFlag: true,
                  listData: listData,
                })
                that.GetGoodsList();
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消');
        }
      }
    })
  },
  // cancelExchange取消兑换专区
  cancelExchange: function (e) {
    var that = this;
    var uid = this.data.uid;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    listData[actionId].actionFlag = true;
    wx.showModal({
      title: '提示',
      content: '该商品取消同步至兑换专区？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=GoodsSetArea&GoodsId=" + id + "&Type=2"),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              // console.log(json);
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                that.setData({
                  maskFlag: true,
                  listData: listData,
                })
                that.GetGoodsList();
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消');
        }
      }
    })
  },
  // 取消付款
  cancelPay: function () {
    this.setData({
      payFlag: true
    })
  },
  //余额或微信 
  radioChange: function (e) {
    var payType = this.data.payType;
    payType = e.detail.value
    this.setData({
      payType: payType,
    });
  },
  // 付款
  goPay: function () {
    var that = this;
    var uid = this.data.uid;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == "") {
      wx.showToast({
        title: "请选择支付方式",
        icon: 'none',
        duration: 2000
      });
    }
    if (payType == '0') {
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true,
          payFlag: true
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=BusinessHalfPowerPay&OrderNumber=" + orderNumber + "&PayType=2"),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //     that.GetUserInfoByID();
            //     that.GetGoodsList();
            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }


        }
      })
    }

  },
})